﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDFL.Helper
{
    public class StrHelper
    {
        /// <summary>
        ///  a function to calculate the max count of continuous characters. 
        ///  For example, input “abb” and it will return 2, input “abbbcc” and it will return 3, etc..
        /// </summary>
        /// <hirtory>
        ///     [Dylan] 08/17/2009  Fix bug, if max list exist in the end, can't got it. e.g. "affddccccc"
        ///     [Dylan] 08/17/2009  Fix bug, if only one continuous list, and in the end, DupList is null, e.g. "aff"
        ///     [Dylan] 08/17/2009  Fix bug, if only one char. e.g "a"
        /// </hirtory>
        /// <param name="str"></param>
        /// <returns>max count of continuous characters</returns>
        public static Int32 MaxofDupCharacter(string str)
        {
            int length = str.Length;
            bool isDup = false;
            int iPre = 0; 
            int iLatter = 1;
            List<int> DupList = new List<int>();

            // [Dylan] 08/17/2009  Fix bug, if only one char. e.g "a"
            if (str.Length == 1)
                return 1;

            for (int i = 0; i < length -1; i++)
            {
                if (str[iLatter] == str[iPre])
                {
                    isDup = true;
                    // [Dylan] 08/17/2009  Fix bug, if only one continuous list, and in the end, DupList is null, e.g. "aff"
                    if (iLatter == length -1 && DupList.Count == 0)
                    {
                        DupList.Add(iLatter - iPre + 1);
                    }
                    // [Dylan] 08/17/2009  Fix bug, if max list exist in the end, can't got it. e.g. "affddccccc"
                    // if true, it indicate that end region is still a continuous list.
                    if (iPre< iLatter -1)
                    {
                        DupList.Add(iLatter - iPre+1);
                    }
                }
                else
                {
                    if (isDup)
                    {
                        DupList.Add(iLatter - iPre);                        
                    }
                    isDup = false;
                    iPre = iLatter;                 
                }
                iLatter++;
            }
            

            return int.Parse(DupList.Max().ToString());
        }


        /// <summary>
        ///         Convert a decimal to the binary format.
        ///         like: 
        ///         stra = Convert.ToString(4, 2).PadLeft(8, '0');
        ///         //stra=00000100
        /// </summary>
        /// <param name="DecimalNum"></param>
        /// <returns></returns>
        public static string ToBinary(Int64 decimalNum)
        {
            Int64 binaryHolder;
            char[] binaryArray;
            string binaryResult = "";

            // fix issue#5943: StrHelper.ToBinary(0)  result "", it should be 0
            if (decimalNum == 0)
            {
                return "0";
            }
            // end fix issue#5943

            while (decimalNum > 0)
            {
                binaryHolder = decimalNum % 2;
                binaryResult += binaryHolder;
                decimalNum = decimalNum / 2;
            }

            // rever the binaryResult, e.g. we get 1101111, then revert to 1111011, this is the final result
            binaryArray = binaryResult.ToCharArray();
            Array.Reverse(binaryArray);
            binaryResult = new string(binaryArray);
            return binaryResult;
        }        
    }
}
